package bot;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import bot.FullCycleBot.OpenSiteThread;

import gui.BotGui;

public class HistoryRetreivalMain {
	public static void main(String[] args){
		final Stack<String> urls = new Stack<String>();
		try{
			BufferedReader reader = new BufferedReader(new FileReader(BotGui.dataNeeded));
			String line;
			while ((line = reader.readLine()) != null){
				if (line.split("http").length == 2){
					String history = line.substring(0, line.length() - 4) + "history";
					urls.push(line);
					urls.push(history);
				}
			}
			reader.close();
		} 
		catch (FileNotFoundException e){
			e.printStackTrace();
		} 
		catch (IOException e){
			e.printStackTrace();
		}
		
		JFrame frame = new JFrame();
		JPanel panel = new JPanel(new GridBagLayout());
		final JButton next = new JButton("Next");
		final JTextArea text = new JTextArea();
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 1;
		gbc.weighty = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		panel.add(next, gbc);
		
		gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.fill = GridBagConstraints.BOTH;
		panel.add(text, gbc);
		
		frame.add(panel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(250, 450);
		frame.setVisible(true);
		
		next.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0){
				HistoryRetreivalMain x = new HistoryRetreivalMain();
				String url = urls.pop();
				System.out.println(url.substring(url.length() - 4, url.length()));
				Thread thread = new Thread(x.new OpenSiteRunnable(url));
				thread.start();
				try{
					thread.join();
				} 
				catch (InterruptedException e){
					e.printStackTrace();
				}
				
				long start = System.currentTimeMillis();
				while (System.currentTimeMillis() - start < 400);
				if (url.substring(url.length() - 4, url.length()).equals("edit")){
					Util.setPasteBuffer(text.getText());
				}
				else{
					text.setText("");
				}
			}
		});
	}
	
	public class OpenSiteRunnable implements Runnable{

		private String url;

		public OpenSiteRunnable(String url){
			this.url = url;
		}

		@Override
		public void run() {
			try {
				Robot robo = new Robot();
				robo.mouseMove(300, 65);
				robo.mousePress(InputEvent.BUTTON1_MASK);
				robo.mouseRelease(InputEvent.BUTTON1_MASK);
				//highlight and then delete the url currently in the browser
				robo.keyPress(KeyEvent.VK_CONTROL);
				robo.keyPress(KeyEvent.VK_A);
				robo.keyRelease(KeyEvent.VK_A);
				robo.keyRelease(KeyEvent.VK_CONTROL);
				robo.keyPress(KeyEvent.VK_DELETE);
				robo.keyRelease(KeyEvent.VK_DELETE);
				//put the url on the paste buffer then paste it in
				Util.setPasteBuffer(url);
				robo.keyPress(KeyEvent.VK_CONTROL);
				robo.keyPress(KeyEvent.VK_V);
				robo.keyRelease(KeyEvent.VK_V);
				robo.keyRelease(KeyEvent.VK_CONTROL);
				//submit the url
				robo.keyPress(KeyEvent.VK_ENTER);
				robo.keyRelease(KeyEvent.VK_ENTER);

			} catch (AWTException e) {
				e.printStackTrace();
			}
		}
	}
}
